<?php

namespace app\common\service\order;

use app\common\model\User as UserModel;
use app\common\model\Wxapp as WxappModel;
use app\common\model\user\MoneyLog as MoneyLogModel;
use app\common\model\OrderRefundLog as OrderRefundLogModel;
use app\common\enum\order\PayType as PayTypeEnum;
use app\common\enum\user\moneylog\Scene as SceneEnum;
use app\common\library\wechat\WxPay;
use think\Db;
/**
 * 订单退款服务类
 * Class Refund
 * @package app\common\service\order
 */
class Refund
{
    /**
     * 执行订单退款
     * @param \app\common\model\BaseModel $order 订单信息
     * @param double|null $money 指定退款金额
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \app\common\exception\BaseException
     */
    public function execute($order, $money = null,$order_refund_id)
    {

        // 退款金额，如不指定则默认为订单实付款金额
        is_null($money) && $money = $order['pay_price'];
        $order_no = $order['order_no'];
        //查询
        $divide_receives = db('divide_receives')->where(['order_no'=>$order_no,'wxapp_id'=>$order['wxapp_id']])->find();

        // 1.微信支付退款
        if ($order['pay_type']['value'] == PayTypeEnum::WECHAT) {
            if(!$divide_receives){
                $log = [
                    'order_id'=>$order['order_id'],
                    'refund_money'=>$money,
                    'wxapp_id'=>$order['wxapp_id'],
                    'user_id'=>$order['user_id'],
                    'pay_type'=>$order['pay_type']['value'],
                ];
                $this->wxpay($order, $money);
                OrderRefundLogModel::add($log);
            }
        }
        // 2.余额支付退款
        if ($order['pay_type']['value'] == PayTypeEnum::BALANCE) {
            if($order['pay_way']==10){
                return $this->balance($order, $money,$order_refund_id);
            }else{
                return $this->shouyi($order, $money,$order_refund_id);
            }

        }
        return false;
    }
    /**
     * 余额支付退款(收益)
     */
    private function shouyi(&$order, $money,$order_refund_id){
        // 回退分红余额
        Db::name('dealer_user')->where(['user_id'=>$order['user_id']])->setInc('money',$money);
        Db::name('order_refund')->where(['order_refund_id'=>$order_refund_id])->update(['refund_money'=>$money]);
        // 记录余额明细
        Db::name('dealer_capital')->insert([
            'user_id'  =>$order['user_id'],
            'flow_type'=>80,
            'money'    =>$money,
            'describe' =>'退款'
        ]);

        $log = [
            'order_id'=>$order['order_id'],
            'refund_money'=>$money,
            'wxapp_id'=>$order['wxapp_id'],
            'user_id'=>$order['user_id'],
            'pay_type'=>$order['pay_type']['value'],
        ];
        OrderRefundLogModel::add($log);
        return true;
    }
    /**
     * 余额支付退款(储蓄)
     * @param $order
     * @param $money
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    private function balance(&$order, $money,$order_refund_id)
    {
        // 回退用户余额
        $user = UserModel::detail($order['user_id']);
        if (empty($user)) return false;
        $user->setInc('money', $money);
        //更新退款记录表
        Db::name('order_refund')->where(['order_refund_id'=>$order_refund_id])->update(['refund_money'=>$money]);
        // 记录余额明细
        MoneyLogModel::add([
            'scene'=> SceneEnum::REFUND,
            'user_id' => $user['user_id'],
            'money' => $money,
            'order_no' => $order['order_no'],
        ]);

        $log = [
            'order_id'=>$order['order_id'],
            'refund_money'=>$money,
            'wxapp_id'=>$order['wxapp_id'],
            'user_id'=>$order['user_id'],
            'pay_type'=>$order['pay_type']['value'],
        ];
        OrderRefundLogModel::add($log);
        return true;
    }

    /**
     * 微信支付退款
     * @param $order
     * @param double $money
     * @return bool
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    private function wxpay(&$order, $money)
    {
        $wxConfig = WxappModel::getWxappCache($order['wxapp_id']);
        $WxPay = new WxPay($wxConfig);
        return $WxPay->refund($order['transaction_id'], $order['pay_price'], $money);
    }

}